package com.ethink.warehouse.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ethink.warehouse.api.info.MateSkuAndMateSkuNameInfo;
import com.ethink.warehouse.service.MateInventoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ethink.framework.common.db.PageUtils;
import com.ethink.warehouse.api.info.MateInventoryOtherInLogDetailInfo;
import com.ethink.warehouse.api.request.MateInventoryOtherInLogDetailAddRequest;
import com.ethink.warehouse.api.request.MateInventoryOtherInLogDetailQueryRequest;
import com.ethink.warehouse.api.request.MateInventoryOtherInLogDetailUpdateRequest;
import com.ethink.warehouse.api.response.MateInventoryOtherInLogDetailAddResponse;
import com.ethink.warehouse.api.response.MateInventoryOtherInLogDetailDeleteResponse;
import com.ethink.warehouse.api.response.MateInventoryOtherInLogDetailUpdateResponse;
import com.ethink.warehouse.common.entity.MateInventoryOtherInLogDetail;
import com.ethink.warehouse.common.mapper.MateInventoryOtherInLogDetailMapper;
import com.ethink.warehouse.converter.MateInventoryOtherInLogDetailConverter;
import com.ethink.warehouse.service.MateInventoryOtherInLogDetailService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;

/**
 * 其他入库明细 Service接口实现
 *
 * @author default
 * @date 2023-02-09
 */
@Slf4j
@Service
public class MateInventoryOtherInLogDetailServiceImpl extends ServiceImpl<MateInventoryOtherInLogDetailMapper, MateInventoryOtherInLogDetail> implements MateInventoryOtherInLogDetailService {

    @Autowired
    private MateInventoryOtherInLogDetailMapper mateInventoryOtherInLogDetailMapper;

    @Autowired
    private MateInventoryOtherInLogDetailConverter mateInventoryOtherInLogDetailConverter;

    @Autowired
    private MateInventoryService mateInventoryService;

    @Override
    public Page<MateInventoryOtherInLogDetailInfo> queryMateInventoryOtherInLogDetail(MateInventoryOtherInLogDetailQueryRequest request) {
        return mateInventoryOtherInLogDetailMapper.queryMateInventoryOtherInLogDetail(PageUtils.buildPage(request), request);
    }

    @Override
    public MateInventoryOtherInLogDetailInfo getMateInventoryOtherInLogDetailById(Long mateInventoryOtherInLogDetailId) {
        return mateInventoryOtherInLogDetailMapper.getMateInventoryOtherInLogDetailById(mateInventoryOtherInLogDetailId);
    }

    @Override
    public List<MateInventoryOtherInLogDetailInfo> listMateInventoryOtherInLogDetail(MateInventoryOtherInLogDetailQueryRequest request) {
        return mateInventoryOtherInLogDetailMapper.listMateInventoryOtherInLogDetail(request);
    }

    @Override
    public MateInventoryOtherInLogDetailAddResponse addMateInventoryOtherInLogDetail(MateInventoryOtherInLogDetailAddRequest request) {
        MateInventoryOtherInLogDetailAddResponse response = new MateInventoryOtherInLogDetailAddResponse();
        MateInventoryOtherInLogDetail mateInventoryOtherInLogDetail = new MateInventoryOtherInLogDetail();
        BeanUtils.copyProperties(request, mateInventoryOtherInLogDetail);
        // FIXME set默认字段
        mateInventoryOtherInLogDetailMapper.insert(mateInventoryOtherInLogDetail);
        return response;
    }

    @Override
    public MateInventoryOtherInLogDetailUpdateResponse updateMateInventoryOtherInLogDetail(MateInventoryOtherInLogDetailUpdateRequest request) {
        MateInventoryOtherInLogDetailUpdateResponse response = new MateInventoryOtherInLogDetailUpdateResponse();
        LambdaUpdateWrapper<MateInventoryOtherInLogDetail> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(MateInventoryOtherInLogDetail::getId, request.getId());
        // FIXME 按实际业务需求set
        updateWrapper.set(MateInventoryOtherInLogDetail::getOrderNo, request.getOrderNo());
        updateWrapper.set(MateInventoryOtherInLogDetail::getMateSku, request.getMateSku());
        updateWrapper.set(MateInventoryOtherInLogDetail::getQty, request.getQty());
        updateWrapper.set(MateInventoryOtherInLogDetail::getCostPrice, request.getCostPrice());
        updateWrapper.set(MateInventoryOtherInLogDetail::getProduceCode, request.getProduceCode());
        updateWrapper.set(MateInventoryOtherInLogDetail::getRkTime, request.getRkTime());
        updateWrapper.set(MateInventoryOtherInLogDetail::getCountryOfOrigin, request.getCountryOfOrigin());
        updateWrapper.set(MateInventoryOtherInLogDetail::getProductDate, request.getProductDate());
        updateWrapper.set(MateInventoryOtherInLogDetail::getExpireDate, request.getExpireDate());
        updateWrapper.set(MateInventoryOtherInLogDetail::getPurchaseCode, request.getPurchaseCode());
        updateWrapper.set(MateInventoryOtherInLogDetail::getMateInventoryId, request.getMateInventoryId());
        mateInventoryOtherInLogDetailMapper.update(null, updateWrapper);
        return response;
    }

    @Override
    public MateInventoryOtherInLogDetailDeleteResponse deleteMateInventoryOtherInLogDetail(Long mateInventoryOtherInLogDetailId) {
        MateInventoryOtherInLogDetailDeleteResponse response = new MateInventoryOtherInLogDetailDeleteResponse();
        LambdaUpdateWrapper<MateInventoryOtherInLogDetail> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(MateInventoryOtherInLogDetail::getId, mateInventoryOtherInLogDetailId);
        updateWrapper.set(MateInventoryOtherInLogDetail::getIsDeleted, true);
        mateInventoryOtherInLogDetailMapper.update(null, updateWrapper);
        return response;
    }

    /**
     * 根据orderNo查询明细数据
     *
     * @param orderNo
     * @return
     */
    @Override
    public List<MateInventoryOtherInLogDetailInfo> listMateInventoryOtherInLogDetailByOrderNo(String orderNo) {
        List<MateInventoryOtherInLogDetail> childrenList = mateInventoryOtherInLogDetailMapper.selectList(Wrappers.<MateInventoryOtherInLogDetail>lambdaQuery().eq(MateInventoryOtherInLogDetail::getOrderNo, orderNo));
        List<String> mateSkuList = childrenList.stream().map(MateInventoryOtherInLogDetail::getMateSku).collect(Collectors.toList());
        List<MateSkuAndMateSkuNameInfo> mateSkuNameList = mateInventoryService.queryMateSkuNameByMateSku(mateSkuList);
        List<MateInventoryOtherInLogDetailInfo> res = new ArrayList<>();
        childrenList.forEach(item -> {
            MateInventoryOtherInLogDetailInfo children = new MateInventoryOtherInLogDetailInfo();
            BeanUtil.copyProperties(item, children, true);
            mateSkuNameList.forEach(mateSkuName -> {
                if (StrUtil.equals(mateSkuName.getMateSku(), children.getMateSku())) {
                    children.setMateSkuName(mateSkuName.getMateSkuName());
                }
            });
            res.add(children);
        });
        return res;
    }

    /**
     * 逻辑删除
     *
     * @param orderNo
     * @return
     */
    @Override
    public Boolean deleteMateInventoryOtherInLogDetailByOrderNo(String orderNo) {
        int count = mateInventoryOtherInLogDetailMapper.update(null, Wrappers.<MateInventoryOtherInLogDetail>lambdaUpdate()
                .eq(MateInventoryOtherInLogDetail::getOrderNo, orderNo)
                .set(MateInventoryOtherInLogDetail::getIsDeleted, Boolean.TRUE)
        );
        return count > 0;
    }

    /**
     * 新增n个
     *
     * @param childrenList
     * @return
     */
    @Override
    public Boolean addMateInventoryOtherInLogDetailList(List<MateInventoryOtherInLogDetail> childrenList) {
        return this.saveBatch(childrenList);
    }
}
